package cn.summit.log.service.impl;

import java.time.Duration;

import cn.summit.log.dto.LogInfoDTO;
import cn.summit.log.service.AccessLogService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.util.ObjectUtils;

@Slf4j
public class DefaultAccessLogServiceImpl implements AccessLogService {

	@Override
	public void doBefore(LogInfoDTO dto) {
		log.info("请求方法 {}", dto.getClassMethod());
		String ip = dto.getRequest().getHeader("X-Forwarded-For");
		if (ObjectUtils.isEmpty(ip)) {
			ip = dto.getRequest().getRemoteAddr();
		}
		log.info("请求来源 {},方式:{},地址:{}",
				ip,
				dto.getHttpMethod(),
				dto.getUri());
	}

	@Override
	public void doAfter(LogInfoDTO dto) {
		log.info("请求耗时 {}ms", Duration.between(dto.getStartTime(), dto.getEndTime()).toMillis());

	}
}
